/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:47
 */
package org.mechanic.fund.web.product;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.product.Product;
import org.mechanic.fund.service.product.IProductService;
import org.mechanic.fund.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品信息 API
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api(value = "产品信息接口")
@RestController
@RequestMapping(value = "product")
public class ProductController extends BaseController {

    private IProductService productService;

    @Autowired
    public ProductController(IProductService productService) {
        this.productService = productService;
    }

    @ApiOperation(value = "查询产品列表")
    @GetMapping(value = "list", produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "产品名称", required = false) @RequestParam(required = false) String name,
      @ApiParam(value = "产品编码", required = false) @RequestParam(required = false) String code,
      @ApiParam(value = "产品风格类型", required = false) @RequestParam(required = false) String styleId) {
         List<Product> products = productService.getProductList(name, code, styleId);
        return result(products);
    }

    @ApiOperation(value = "返回匹配指定主键编号的产品信息")
    @GetMapping(value = "{id}", produces = MediaTypes.JSON_UTF_8)
    public Object details(
      @ApiParam(value = "产品编号") @PathVariable(required = true) String id) {
         Product product = this.productService.getById(id);
        return result(product);
    }

    @ApiOperation(value = "批量保存产品")
    @GetMapping(value = "save", consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "产品集合", required = true)
      @RequestBody(required = true) List<Product> products) {

        this.productService.save(products, super.currUser().getId());
        return result();
    }

    @ApiOperation(value = "按 ID 删除产品")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE, produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "产品 ID", required = true) @PathVariable(required = true) String id) {
        this.productService.delete(id);
        return result();
    }
}
